package com.scs.application.modules.msg.service.impl;

import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.request.HospitalSupplierRequest;
import com.scs.application.modules.base.service.HospitalSupplierService;
import com.scs.application.modules.base.vo.HospitalSupplierVO;
import com.scs.application.modules.msg.dto.MscmReceiveDTO;
import com.scs.application.modules.msg.entity.MscmPushedRecord;
import com.scs.application.modules.msg.entity.MscmReceive;
import com.scs.application.modules.msg.mapper.MscmReceiveMapper;
import com.scs.application.modules.msg.service.MscmPushedRecordService;
import com.scs.application.modules.msg.service.MscmReceiveService;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @program: fortec-mscm-parent
 * @description:
 **/
@Slf4j
@AllArgsConstructor
@Service
@Transactional(rollbackFor = Exception.class)
public class MscmReceiveServiceImpl extends BaseServiceImpl<MscmReceiveMapper, MscmReceive> implements MscmReceiveService {

    @Autowired
    private HospitalSupplierService hospitalSupplierService;

    private MscmPushedRecordService mscmPushedRecordService;

    public void saveSendToSupplier(MscmReceiveDTO dto) {
        String hospitalId = UserUtils.getHospitalId();
        List<HospitalSupplierVO> hospitalSupplier = hospitalSupplierService.listByHospital(new HospitalSupplierRequest().setHospitalId(hospitalId).setInactive(HospitalSupplierRequest.ACTIVATE));
        List<MscmReceive> listMscmReceive = Lists.newArrayListWithExpectedSize(hospitalSupplier.size());
        List<MscmPushedRecord> listpushedRecord = Lists.newArrayListWithExpectedSize(hospitalSupplier.size());
        for (HospitalSupplierVO h : hospitalSupplier){
            MscmReceive r = new MscmReceive();
            MscmPushedRecord p = new MscmPushedRecord();
            BeanUtils.copyProperties(dto, r);
            BeanUtils.copyProperties(dto, p);
            r.setReceiver(h.getLoginKey());
            p.setReceiver(h.getLoginKey()).setSceneCode(UserUtils.getHospitalId()).setGmtCreate(new Date()).setIsSuccess(MscmPushedRecord.SUCCESS);
            listMscmReceive.add(r);
            listpushedRecord.add(p);
        }
        //保存接收记录
        this.saveBatch(listMscmReceive);
        //保存推送记录
        mscmPushedRecordService.saveBatch(listpushedRecord);
        //修改base hospital_notice 发送状态

    }
}
